package sort

//4.词频统计与排序

import (
	"sort"
	"strings"
	"unicode"
)

// WordFrequency 定义一个结构来存储单词及其频率
type WordFrequency struct {
	FrequencyMap map[string]int
}

// NewWordFrequency 创建一个新的单词频率结构
func NewWordFrequency() *WordFrequency {
	return &WordFrequency{
		FrequencyMap: make(map[string]int),
	}
}

// TokenizeAndUpdate 分词函数，更新单词频率
func (wf *WordFrequency) TokenizeAndUpdate(text string) {
	// 使用 FieldsFunc 函数来自定义分词规则
	tokens := strings.FieldsFunc(text, func(r rune) bool {
		// 根据标点符号和空白字符进行分词
		return unicode.IsSpace(r) || unicode.IsPunct(r)
	})

	// 更新单词频率
	for _, token := range tokens {
		// 将单词转换为小写
		token = strings.ToLower(token)
		// 更新频率
		wf.FrequencyMap[token]++
	}
}

// SortByFrequency 对单词频率进行排序并返回排序后的结果
func (wf *WordFrequency) SortByFrequency() []string {
	type kv struct {
		Word  string
		Count int
	}

	var sortedList []kv
	for word, count := range wf.FrequencyMap {
		sortedList = append(sortedList, kv{word, count})
	}

	// 先按照单词字典序排序，然后按照频率排序
	sort.Slice(sortedList, func(i, j int) bool {
		return sortedList[i].Word < sortedList[j].Word
	})

	sort.SliceStable(sortedList, func(i, j int) bool {
		return sortedList[i].Count > sortedList[j].Count
	})

	var result []string
	for _, item := range sortedList {
		result = append(result, item.Word)
	}

	return result
}
